<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/oj-logo.png" alt="oj" />
            <div class="title">在线刷题</div>
          </div>
        </a-menu-item>

        <a-menu-item v-for="item in visibleRouters" :key="item.path"
          >{{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>

    <a-col flex="100px">
      <a-space>
        <a-button
          type="primary"
          @click="toLogin"
          :disabled="userName != '未登录'"
          >{{ userName }}</a-button
        >
      </a-space>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import AccessEnum from "@/access/accessEnum";

const router = useRouter();
const store = useStore();
const loginUser = store.state.user.loginUser;

//路由跳转时，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

//显示的路由
const visibleRouters = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

const userName = store.state.user?.loginUser?.userName ?? "未登录";
//显示的路由
// const visibleRouters = routes.filter((item, index) => {
//   if (item.meta?.hideInMenu) {
//     return false;
//   }
//   // 根据权限过滤菜单
//   if (!checkAccess(store.state.user.loginUser, item?.meta?.access as string)) {
//     return false;
//   }
//   return true;
// });

//默认主页
const selectedKeys = ref(["/"]);
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

const toLogin = () => {
  router.push({
    path: "/user/login",
  });
};
//console.log(store.state.user);

// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userAccount: "jipi",
//     userPassword: "12345678",
//   });
// }, 3000);
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 2px;
}

.logo {
  width: 60px;
  height: 60px;
}
</style>
